﻿<!DOCTYPE html>
<html lang="en">

<head>
  <meta charset="UTF-8">
  <meta http-equiv="X-UA-Compatible" content="IE=edge">
  <link rel="Shortcut Icon" href="../images/aecc.ico" type="image/x-icon" />
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <link rel="stylesheet" href="../StaticFiles/element-ui/element-ui.css">
  <link rel="stylesheet" href="../StaticFiles/nprogress/nprogress.min.css">
  <link rel="stylesheet" href="../styles/vue-page-common.css">
  <link rel="stylesheet" href="../styles/table-transparent.css">
  <script src="../Scripts/vue.min.js"></script>
  <script src="../StaticFiles/element-ui/element-ui.js"></script>
  <script src="../Scripts/axios.min.js"></script>
  <script src="../StaticFiles/nprogress/nprogress.min.js"></script>
  <script src="../Scripts/crypto-js.js"></script>
  <script src="../Scripts/secret.js"></script>
  <script src="../Scripts/dayjs.min.js"></script>
  <script src="../Scripts/axios.config.js"></script>
  <script src="../Scripts/datav.min.vue.js"></script>
  <script src="../Scripts/lodash.min.js"></script>
  <title>三天锁排回收率统计看板</title>
  <style>
    html {
      background-image: url(../images/bg.jpg);
      color: #fff;
    }

    .header-title {
      font-size: 24px !important;
    }

    .el-form-item__label {
      color: #fff;
    }

    .el-form-item {
      margin-bottom: 0px;
    }

    .chart-title {
      text-align: center;
      padding-top: 10px;
      color: #fff;
    }

    .chart {
      width: 100%;
      font-size: 40px;
      font-weight: bold;
      cursor: pointer;
      text-align: center;
    }
  </style>
</head>

<body>
  <div id="app" v-cloak>
    <div style="padding:0 5px;">
      <!-- 看板标题组件 -->
      <vue2-board-header :title="'三天锁排回收率统计看板'"></vue2-board-header>
      <el-form :inline="true" :model="queryInfo" ref="queryInfoRef" :label-position="'left'" :rules="rules"
        id="queryInfo" style="margin:10px 10px 0 10px;">
        <el-form-item label="计划完成时间:" prop="endtime">
          <el-date-picker v-model="queryInfo.endtime" type="daterange" range-separator="至" value-format="yyyy-MM-dd"
            start-placeholder="开始日期" end-placeholder="结束日期" :picker-options="pickerOptions">
          </el-date-picker>
        </el-form-item>

        <el-form-item label="主制分厂:">
          <el-select v-model="queryInfo.factoryid" placeholder="请选择主制分厂" clearable filterable
            @change="handlerBtnSearch">
            <el-option v-for="f in factoryList" :key="f.factoryid" :label="f.factoryname" :value="f.factoryid">
            </el-option>
          </el-select>
        </el-form-item>

        <el-form-item prop='mfgordername' label="订单号:">
          <el-autocomplete v-model.trim="queryInfo.mfgordername" placeholder="请输入订单号" :trigger-on-focus="false"
            :fetch-suggestions="searchMfgorderAsync" :value-key="'mfgordername'" :debounce="500" style="width:230px;">
          </el-autocomplete>
        </el-form-item>

        <el-form-item prop='productname' label="零件图号:">
          <el-autocomplete v-model.trim="queryInfo.productname" placeholder="请输入零件图号" :trigger-on-focus="false"
            style="width:200px;" :fetch-suggestions="searchProductAsync" :value-key="'productname'" :debounce="500">
          </el-autocomplete>
        </el-form-item>

        <el-form-item prop='containername' label="流水卡号:">
          <el-autocomplete v-model.trim="queryInfo.containername" placeholder="请输入流水卡号" :trigger-on-focus="false"
            style="width:200px;" :fetch-suggestions="searchContainerAsync" :value-key="'containername'" :debounce="500">
          </el-autocomplete>
        </el-form-item>

        <el-form-item>
          <el-button type="primary" icon="el-icon-search" size="small" @click="handlerBtnSearch">查询</el-button>
          <el-button type="primary" icon="el-icon-download" size="small" @click="getExportExcel">导出</el-button>
        </el-form-item>
      </el-form>

      <el-row :gutter="10">
        <el-col :span="18">
          <dv-border-box-10>
            <h3 class="chart-title">排产计划回收率统计</h3>
            <div id="recycleRateData" class="chart" :style="{ height: chartHeight +'px' }"></div>
          </dv-border-box-10>
        </el-col>
        <el-col :span="6">
          <dv-border-box-10>
            <h3 class="chart-title">排产计划回收率统计（合计）</h3>
            <div id="recycleRateTotalData" class="chart" :style="{ height: chartHeight +'px' }"></div>
          </dv-border-box-10>
        </el-col>
      </el-row>

      <dv-border-box-10>
        <h3 class="chart-title">排产计划回收明细数据</h3>
        <div id="datagrid" style="padding:10px;" class="custom-scrollable-element">
          <el-table v-loading="loading" v-loading.fullscreen.lock="loading" :element-loading-text="loadingText"
            element-loading-background="rgba(0,0,0,0.8)" :data="dataList" stripe border height="350">
            <el-table-column type="index" width="50"></el-table-column>
            <el-table-column label="主制厂" prop="factoryname" width="100" show-overflow-tooltip></el-table-column>
            <el-table-column label="中心" prop="workcentername" width="100" show-overflow-tooltip></el-table-column>
            <el-table-column label="单元" prop="teamname" width="100" show-overflow-tooltip></el-table-column>
            <el-table-column label="回收人" prop="fullname" width="100" show-overflow-tooltip></el-table-column>
            <el-table-column label="回收时间" prop="updatedate" width="170" :formatter="replaceDate" show-overflow-tooltip>
            </el-table-column>
            <el-table-column label="型别" prop="familylistnames" width="100" show-overflow-tooltip></el-table-column>
            <el-table-column label="零件号" prop="productname" width="100" show-overflow-tooltip></el-table-column>
            <el-table-column label="订单号" prop="mfgordername" width="100" show-overflow-tooltip></el-table-column>
            <el-table-column label="批次号" prop="containername" show-overflow-tooltip></el-table-column>
            <el-table-column label="工序名称" prop="workflowstepname" width="100" show-overflow-tooltip></el-table-column>
            <el-table-column label="计划完成时间" prop="endtime" width="170" :formatter="replaceDate" show-overflow-tooltip>
            </el-table-column>
            <el-table-column label="工艺路线" prop="workflowname" width="100" show-overflow-tooltip></el-table-column>
            <el-table-column label="工艺版本" prop="workflowrevision" width="100" show-overflow-tooltip></el-table-column>
            <el-table-column label="回收原因" prop="reason" width="100" show-overflow-tooltip></el-table-column>
          </el-table>
          <el-pagination @size-change="handleSizeChange" @current-change="handleCurrentChange"
            :current-page="queryInfo.currentPage" :page-sizes="[10,20,50,100]" :page-size="queryInfo.pageSize"
            layout="prev,pager,next,jumper,sizes,total" :total="total">
          </el-pagination>
        </div>
      </dv-border-box-10>

      <!-- 明细 -->
      <el-dialog :title="chartDetail.queryInfo.title" :width="'98%'" :top="'10px'" :center="'center'"
        :visible.sync="chartDetail.dialogVisiable">
        <el-table v-loading="loading" v-loading.fullscreen.lock="loading" :element-loading-text="loadingText"
          element-loading-background="rgba(0,0,0,0.8)" :data="chartDetail.dataList" stripe border>
          <el-table-column type="index" width="50"></el-table-column>
          <el-table-column label="主制厂" prop="factoryname" width="100" show-overflow-tooltip></el-table-column>
          <el-table-column label="中心" prop="workcentername" width="100" show-overflow-tooltip></el-table-column>
          <el-table-column label="单元" prop="teamname" width="100" show-overflow-tooltip></el-table-column>、
          <el-table-column label="回收人" v-if="seriesName !== '排产工序数'" prop="fullname" width="100"></el-table-column>
          <el-table-column label="回收时间" v-if="seriesName !== '排产工序数'" prop="updatedate" width="170"
            :formatter="replaceDate" show-overflow-tooltip>
          </el-table-column>
          <el-table-column label="型别" prop="familylistnames" width="100" show-overflow-tooltip></el-table-column>
          <el-table-column label="零件号" prop="productname" width="100" show-overflow-tooltip></el-table-column>
          <el-table-column label="订单号" prop="mfgordername" width="100" show-overflow-tooltip></el-table-column>
          <el-table-column label="批次号" prop="containername" show-overflow-tooltip></el-table-column>
          <el-table-column label="工序名称" prop="workflowstepname" width="100" show-overflow-tooltip></el-table-column>
          <el-table-column label="计划开始时间" v-if="seriesName === '排产工序数'" prop="starttime" width="170"
            :formatter="replaceDate" show-overflow-tooltip>
          </el-table-column>
          <el-table-column label="计划完成时间" v-if="seriesName === '排产工序数'" prop="endtime" width="170"
            :formatter="replaceDate" show-overflow-tooltip>
          </el-table-column>
          <el-table-column label="工艺路线" prop="workflowname" width="100" show-overflow-tooltip></el-table-column>
          <el-table-column label="工艺版本" prop="workflowrevision" width="100" show-overflow-tooltip></el-table-column>
          <el-table-column label="操作" width="100">
            <template slot-scope="scope">
              <el-button type="primary" icon="el-icon-download" size="mini" @click="exportChartDetailData">导出
              </el-button>
            </template>
          </el-table-column>
        </el-table>
        <el-pagination @current-change="handleDetailCurrentChange" :current-page="chartDetail.queryInfo.currentPage"
          :page-sizes="[chartDetail.queryInfo.pageSize]" :page-size="chartDetail.queryInfo.pageSize"
          layout="prev,pager,next,jumper,sizes,total" :total="chartDetail.total">
        </el-pagination>
      </el-dialog>
    </div>
  </div>
  <script src="../Scripts/echarts/echarts-5.4.0.min.js"></script>
  <script type="module">
    import { barOption, colorList } from '../Scripts/hooks/chartConfig.js'
    import { exportExcelData, getParams, fetchSuggestion } from '../Scripts/hooks/useCommonMethod.js'
    import { prevWeek, thisWeek, nextWeek, prevMonth, thisMonth, nextMonth } from '../Scripts/hooks/useDateShortCut.js'
    import { getUserInfo, getFactory } from '../Scripts/hooks/fetchAPI.js'
    import { splitDate, replaceDate } from '../Scripts/hooks/format.js'
    import Vue2BoardHeader from '../Scripts/components/Vue2BoardHeader.js'
    const app = new Vue({
      el: '#app',
      components: { Vue2BoardHeader },
      data: {
        splitDate, replaceDate,
        total: 0,
        loading: false,
        loadingText: '数据加载中...',
        thisweek: '',
        queryInfo: {
          factoryid: '',
          endtime: '',//计划完成时间
          mfgordername: '',
          productname: '',
          containername: '',
          currentPage: 1,//当前页
          pageSize: 10//每页显示条数
        },
        userInfo: {},
        loadingConfig: { text: '数据查询中...', effect: 'whirling', maskColor: 'transparent', textColor: '#fff' },
        rules: {
          endtime: [
            { required: true, message: '【计划完成时间】不能为空', trigger: 'change' }
          ],
        },
        dataList: [],
        factoryList: [],
        pickerOptions: { shortcuts: [prevWeek, thisWeek, nextWeek, prevMonth, thisMonth, nextMonth] },
        baseApi: '../handler/board/APSPlanDataRecycleDashBoard.ashx',
        chartHeight: window.innerHeight - 670,
        chartDetail: {
          dialogVisiable: false,
          queryInfo: { seriesName: '', deptname: '', title: '', currentPage: 1, pageSize: 12 },
          dataList: [],
          total: 0
        },
        seriesName: '排产工序数',
      },
      methods: {
        // 查询订单号
        searchMfgorderAsync(value, callback) {
          fetchSuggestion({ actionName: 'getMfgordernameAsync', name: 'mfgordername', value: value.trim(), callback });
        },
        // 查询件号
        searchProductAsync(value, callback) {
          fetchSuggestion({ actionName: 'getProductnameAsync', name: 'productname', value: value.trim(), callback });
        },
        // 查询批次号
        async searchContainerAsync(value, callback) {
          fetchSuggestion({ actionName: 'getContainernameAsync', name: 'containername', value: value.trim(), callback });
        },
        // 查询按钮
        handlerBtnSearch() {
          this.$refs['queryInfoRef'].validate((valid) => {
            if (valid) {
              this.getRecycleRateData();
              this.getData();
            }
          })
        },
        // 排产计划回收率
        async getRecycleRateData() {
          const dom = document.getElementById("recycleRateData");
          echarts.dispose(dom);
          let chart = echarts.init(dom);
          chart.showLoading(this.loadingConfig);

          const dom2 = document.getElementById("recycleRateTotalData");
          echarts.dispose(dom2);
          let chart2 = echarts.init(dom2);
          chart2.showLoading(this.loadingConfig);

          const { data: { data, meta, total } } = await axios.post(`${this.baseApi}?action=getChangeRateData`, {
            type: 'getRecycleRateData',
            ...this.queryInfo
          })

          chart.hideLoading();
          chart2.hideLoading();

          if (meta.status === 500) { return this.$message.error(meta.msg); }
          if (data.length === 0) { return; }

          echarts.dispose(dom);
          echarts.dispose(dom2);

          chart = echarts.init(dom);
          chart2 = echarts.init(dom2);

          let arrFactory = [], arrApsqty = [], arrChangeqty = [], arrChangeRate = [];
          for (const i of data) {
            arrFactory.push(i.deptname);
            arrApsqty.push(i.apsqty);
            arrChangeqty.push(i.changeqty);
            arrChangeRate.push(i.changerate);
          }

          let option = barOption({ isLableFormat: true, provideNumber: 4 });
          option.legend.data = ['排产工序数', '计划回收数', '计划回收率'];
          option.xAxis.data = arrFactory;
          option.yAxis[1].axisLabel.formatter = function (data) { return data + "%"; };

          option.series[0] = _.merge(option.series[0], {
            name: '排产工序数',
            data: arrApsqty,
            label: { color: colorList.blue.color },
            itemStyle: { color: { colorStops: colorList.blue.colorStops } }
          });

          option.series[1] = _.merge(_.cloneDeep(option.series[0]), {
            name: '计划回收数',
            data: arrChangeqty,
            label: { color: colorList.green.color },
            itemStyle: { color: { colorStops: colorList.green.colorStops } }
          });

          option.series[2] = _.merge(_.cloneDeep(option.series[0]), {
            name: '计划回收率',
            yAxisIndex: 1,
            data: arrChangeRate,
            label: { color: colorList.red.color, formatter: '{c}%' },
            itemStyle: { color: { colorStops: colorList.red.colorStops } }
          });

          chart.setOption(option);
          window.addEventListener('resize', () => {
            chart.resize();
          });

          let totalApsQty = arrApsqty.reduceRight((sum, current) => sum + current, 0);// 排产工序数【合计】
          let totalChangeQty = arrChangeqty.reduceRight((sum, current) => sum + current, 0);// 计划变更数【合计】
          let totalChangeRate = ((totalChangeQty / totalApsQty) * 100).toFixed(2);
          let labelName = '';
          if (this.queryInfo.factoryid) {
            labelName = this.factoryList.filter(i => i.factoryid === this.queryInfo.factoryid)[0].factoryname;
          } else {
            labelName = '黎阳公司';
          }

          let option2 = barOption();
          option2.legend.data = ['排产工序数', '计划回收数', '计划回收率'];
          option2.xAxis.data = [labelName];
          option2.yAxis[1].axisLabel.formatter = function (data) { return data + "%"; };

          option2.series[0] = _.merge(option2.series[0], {
            name: '排产工序数',
            data: [totalApsQty],
            label: { color: colorList.blue.color },
            itemStyle: { color: { colorStops: colorList.blue.colorStops } }
          });

          option2.series[1] = _.merge(_.cloneDeep(option2.series[0]), {
            name: '计划回收数',
            data: [totalChangeQty],
            label: { color: colorList.green.color },
            itemStyle: { color: { colorStops: colorList.green.colorStops } }
          });

          option2.series[2] = _.merge(_.cloneDeep(option2.series[0]), {
            name: '计划回收率',
            yAxisIndex: 1,
            data: [totalChangeRate],
            label: { color: colorList.red.color, formatter: '{c}%' },
            itemStyle: { color: { colorStops: colorList.red.colorStops } }
          });

          chart2.setOption(option2);
          window.addEventListener('resize', () => {
            chart2.resize();
          });

          chart.on('click', (params) => {
            const { seriesName, name, value } = params;
            if (value == 0) { return; }
            this.seriesName = seriesName;
            this.chartDetail.queryInfo.deptname = name;//单位名称
            this.chartDetail.queryInfo.seriesName = seriesName;//系列名称
            this.chartDetail.queryInfo.currentPage = 1;
            this.chartDetail.queryInfo.title = `${name}--【${seriesName}】明细数据`;
            this.chartDetail.dialogVisiable = true;
            this.getDetailsData();
          })
        },
        // 主界面表格
        async getData() {
          this.loading = true;
          this.loadingText = '数据查询中...';
          const { data: { data, meta, total } } = await axios({
            method: 'post',
            url: `${this.baseApi}?action=getData`,
            data: { type: 'getData', ...this.queryInfo }
          })
          this.loading = false;
          if (meta.status === 500) { return this.$message.error(meta.msg); }
          this.dataList = data;
          this.total = total;
        },
        handleCurrentChange(newPage) {
          this.queryInfo.currentPage = newPage;
          this.getData();
        },
        handleSizeChange(pageSize) {
          this.queryInfo.pageSize = pageSize;
          this.getData();
        },
        // 数据钻取
        async getDetailsData() {
          this.loading = true;
          this.loadingText = '数据加载中...';
          const { data: { data, meta, total } } = await axios.post(`${this.baseApi}?action=getDetailsData`, {
            type: 'getDetailsData',
            ...this.queryInfo,
            ...this.chartDetail.queryInfo
          })
          this.loading = false;
          if (meta.status === 500) { return this.$message.error(meta.msg); }
          this.chartDetail.dataList = data;
          this.chartDetail.total = total;
        },
        // 数据钻取分页
        handleDetailCurrentChange(newPage) {
          this.chartDetail.queryInfo.currentPage = newPage;
          this.getDetailsData();
        },
        // 主界面数据导出
        async getExportExcel() {
          this.$refs['queryInfoRef'].validate(async (valid) => {
            if (valid) {
              const config = {
                url: `${this.baseApi}?action=exportExcel`,
                data: { type: 'exportExcel', ...this.queryInfo }
              }
              exportExcelData(config, this);
            }
          })
        },
        // 弹窗明细导出
        exportChartDetailData() {
          const config = {
            url: `${this.baseApi}?action=exportDetailsData`,
            data: {
              type: 'exportDetailsData',
              ...this.queryInfo,
              ...this.chartDetail.queryInfo
            }
          }
          exportExcelData(config, this);
        }
      },
      async mounted() {
        window.addEventListener('resize', () => {
          this.chartHeight = window.innerHeight - 670;
        });
        const [userInfo, factoryList] = await Promise.all([
          getUserInfo({ employeeid: getParams("uid") }),
          getFactory()
        ]);

        this.userInfo = userInfo;
        this.factoryList = factoryList;

        if (this.userInfo.factoryid && this.factoryList.some(item => item.factoryid === this.userInfo.factoryid)) {
          const { factoryid } = this.userInfo;
          this.queryInfo.factoryid = factoryid;
        }

        const startOfThisMonth = dayjs().startOf('month').format('YYYY-MM-DD');
        const endOfThisMonth = dayjs().endOf('month').format('YYYY-MM-DD');
        this.queryInfo.endtime = [startOfThisMonth, endOfThisMonth];
        this.handlerBtnSearch();
      }
    })
  </script>
</body>

</html>